<!-- 登录页面:login -->
<template>
  <div :class="[
      'login-wrapper',
      ['', 'login-form-right', 'login-form-left'][direction]
    ]"
       class="flex"
       :style="indexData.loginBackground ? { 'background-image': 'url(' + indexData.loginBackground + ')' } : { 'background-image': 'url(' + backgrounds + ')' } ">
    <div class="flex-column"
         style="z-index: 99;">
      <!--       :style="{backgroundImage:`url(${require('/src/assets/loginBj.jpg')})`}"-->
      <div class="login-form2 ele-bg-white flex-align-center"
           style="border-radius: 11px;box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);">
        <img :src="indexData.loginLeftIcon ? indexData.loginLeftIcon : leftIcons"
             alt="logo"
             style="height: 480px;border-radius: 8px 0px 0px 8px;padding:5px;" />
        <el-form ref="form"
                 size="large"
                 :model="form"
                 :rules="rules"
                 class="login-form ele-bg-white"
                 @keyup.enter.native="submit">
          <!--‘用户登录’ 标题-->
          <h4>{{ $t('login.title') }}</h4>
          <el-form-item prop="username">
            <el-input clearable
                      v-model="form.username"
                      prefix-icon="el-icon-user"
                      :placeholder="$t('login.username')" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input show-password
                      v-model="form.password"
                      prefix-icon="el-icon-lock"
                      :placeholder="$t('login.password')" />
          </el-form-item>
          <el-form-item prop="code">
            <div class="login-input-group">
              <el-input clearable
                        v-model="form.code"
                        prefix-icon="el-icon-_vercode"
                        :placeholder="$t('login.code')" />
              <!--图形验证码-->
              <img alt=""
                   v-if="captcha"
                   :src="captcha"
                   class="login-captcha"
                   @click="changeCaptcha" />
            </div>
          </el-form-item>
          <div class="el-form-item">
            <!--<el-link
              type="primary"
              :underline="false"
              class="ele-pull-right"
              @click="$router.push('/forget')"
            >
              忘记密码
            </el-link>-->
          </div>
          <!--确定登录按钮-->
          <div class="el-form-item">
            <el-button size="large"
                       type="primary"
                       class="login-btn"
                       :loading="loading"
                       @click="submit">
              {{ loading ? $t('login.loading') : $t('login.login') }}
            </el-button>
          </div>
        </el-form>

      </div>

      <div class="d-fixed flex">
        <div>
          <div class="d-title"
               v-if="isOnLine==true">在线客服</div>
          <div class="flex-align-center d-height">
            <img v-if="indexData.serviceQrCode"
                 :src="indexData.serviceQrCode"
                 class="d-img"
                 id="img-fangda" />
            <div class="flex-columns">
              <div class="d-div m-bottom10"
                   v-if="indexData.attendanceTime">在线时间：{{indexData.attendanceTime}}</div>
              <div class="d-div m-bottom10">
                <a href="https://beian.miit.gov.cn/"
                   target="_blank"> {{indexData.theRecord}}</a>
              </div>
              <div class="d-div m-bottom10">
                {{indexData.forTheRecord}}
              </div>
              <div class="d-div m-bottom10">
                {{indexData.versionInformation}}
              </div>
            </div>
          </div>
        </div>
        <div class="flex-columns"
             style="margin-left: 126px;">
          <div class="d-title"
               v-if="isTime==true">联系我们</div>
          <div class="d-height">
            <div class="flex-align-center d-margin-btn"
                 v-if="indexData.phone">
              <el-icon class="el-icon-phone-outline"></el-icon>
              <div class="d-div2">{{indexData.phone}}</div>
            </div>
            <div class="flex-align-center d-margin-btn"
                 v-if="indexData.mailbox">
              <el-icon class="el-icon-_network"></el-icon>
              <div class="d-div2">{{indexData.mailbox}}.com</div>
            </div>
            <div class="flex-align-center d-margin-btn"
                 v-if="indexData.address">
              <el-icon class="el-icon-location-outline"></el-icon>
              <div class="d-div2">{{indexData.address}}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 多语言切换 -->
    <div style="position: absolute; right: 30px; top: 20px">
      <i18n-icon :icon-style="{ fontSize: '22px', color: '#fff', cursor: 'pointer' }" />
    </div>
  </div>
</template>

<script>
// import "../../assets/js/canvas-nest.min";
// import "../../assets/js/jigsaw.js";
import { getAddressConfiguration } from '@/api/agent/pointAllocation';
import I18nIcon from '@/layout/components/i18n-icon.vue';
import { getToken } from '@/utils/token-util';
import { login, getCaptcha } from '@/api/login';
import { JSEncrypt } from 'jsencrypt'

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Login',
  components: { I18nIcon },
  data () {
    return {
      isOnLine: false,
      isTime: false,
      // 修改
      indexData: {
        logo_title: '',
        site_login_image: '',
        site_login_background: '',
        site_login_image: ''
      },
      // 登录框方向, 0居中, 1居右, 2居左
      direction: 0,
      // 加载状态
      loading: false,
      // 表单数据
      form: {
        username: '',
        password: '',
        code: ''
      },
      // 验证码base64数据
      captcha: '',
      // 验证码内容, 实际项目去掉
      text: '',
      loginInfo: {},
      backgrounds: '',
      leftIcons: ''
    };
  },
  computed: {
    // 表单验证规则
    rules () {
      return {
        username: [
          {
            required: true,
            message: this.$t('login.username'),
            type: 'string',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: this.$t('login.password'),
            type: 'string',
            trigger: 'blur'
          }
        ]
      };
    }
  },
  created () {
    // let host = 1
    let host = window.location.host
    // 1 :总端 2:商家端 3：代理端  ———— 默认登录背景+左侧图片
    this.backgrounds = host == 'dspkk.weijuyunke.com' ? require('/src/assets/allBj.png') : host == 'oemkk.weijuyunke.com' ? require('/src/assets/loginBj.jpg') : require('/src/assets/agentBj.jpg')
    this.leftIcons = host == 'dspkk.weijuyunke.com' ? require('/src/assets/allLeft2.png') : host == 'oemkk.weijuyunke.com' ? require('/src/assets/leftIcon2.png') : require('/src/assets/agentLeft.jpg')
    console.log("window==========", 'http://' + host.split(':')[0])
    if (getToken()) {
      this.goHome();
    } else {
      this.changeCaptcha();
    }
  },
  mounted () {
    var host = window.location.host;
    console.log("host=====", host)
    // 查询回显
    getAddressConfiguration({
      // oemId:this.$store.state.user.info.oemId,
      // proxyAddress:'https://yuntt.weijuyunke.com'
      proxyAddress: 'https://' + host //域名
    }).then((msg) => {
      if (msg.data != null) {
        console.log("msg=========", msg)
        this.indexData = msg.data;
        this.isOnLine = this.indexData.serviceQrCode || this.indexData.attendanceTime || this.indexData.theRecord || this.indexData.forTheRecord || this.indexData.versionInformation ? true : false
        this.isTime = this.indexData.phone || this.indexData.mailbox || this.indexData.address ? true : false
        localStorage.setItem('indexData', JSON.stringify(this.indexData))// 存储 “系统图标等”
        let item = JSON.parse(localStorage.getItem('indexData'))
        // 设置动态标题
        document.title = item.systemName
        // 设置动态link图标（icon)
        var link = document.querySelector("link[rel*='icon']") || document.createElement('link')
        link.type = 'image/x-icon'
        link.rel = 'shortcut icon'
        link.href = item.browserIcon ? item.browserIcon : require('@/assets/browser.svg')
        document.getElementsByTagName('head')[0].appendChild(link)
      } else {
        // 设置动态link图标（icon)
        var link = document.querySelector("link[rel*='icon']") || document.createElement('link')
        link.type = 'image/x-icon'
        link.rel = 'shortcut icon'
        link.href = require('@/assets/browser.svg')
        document.getElementsByTagName('head')[0].appendChild(link)
      }
    })
      .catch((e) => {
        this.$message.error(e.message);
      });
  },
  methods: {
    /* 提交 */
    submit () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        let _form = this.form
        _form = JSON.stringify(_form)
        _form = JSON.parse(_form)
        let encryptor = new JSEncrypt()
        let pk = this.$store.state.signature.pk
        console.log("pk", pk)
        encryptor.setPublicKey(pk)
        _form.password = encryptor.encrypt(_form.password)
        login(_form)
          .then((msg) => {
            this.$message.success(msg);
            this.goHome();
          })
          .catch((e) => {
            this.loading = false;
            this.changeCaptcha()
            this.$message.error(e.message);
          });
      });
    },
    /* 跳转到首页 */
    goHome () {
      let a = this.$route?.query?.from ?? '/'
      console.log('a', a, 'this.$route', this.$route)
      console.log("跳转到首页", this.$route, '.query', this.$route.query, 'from', this.$route.from)
      // 目前进入‘/’
      this.$router.push(this.$route?.query?.from ?? '/').catch(() => { });
      // this.loading = false;  隐藏是因为跳转至首页时间太久，因此为了避免重复点击，不做false操作

    },
    /* 更换图形验证码 */
    changeCaptcha () {
      // 这里演示的验证码是后端返回base64格式的形式, 如果后端地址直接是图片请参考忘记密码页面
      getCaptcha()
        .then((data) => {
          this.captcha = data.captcherImg;
          // 实际项目后端一般会返回验证码的key而不是直接返回验证码的内容, 登录用key去验证, 可以根据自己后端接口修改
          this.form.userKey = data.userKey;
          // 自动回填验证码, 实际项目去掉这个
          // this.form.code = this.text;
          this.$refs?.form?.clearValidate();
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
/* 背景 */
.login-wrapper {
  padding: 50px 20px;
  position: relative;
  box-sizing: border-box;
  /*background-image: url('@/assets/bg-login.jpg');*/
  background-image: url('@/assets/loginBj.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;

  &:before {
    content: '';
    /*background-color: rgba(0, 0, 0, 0.2);*/
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
}

login-form2 {
  /* position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -220px;*/

  background-color: #fff;
  max-width: 100%;
  margin: 0 auto;
  padding: 25px 30px;
  position: relative;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  box-sizing: border-box;
  border-radius: 4px;
  z-index: 2;
}

/* 底部信息 start*/
@media screen and (min-height: 900px) {
  .d-fixed {
    width: 92%;
    bottom: 20px;
    left: 20px;
    position: fixed;
    z-index: 9999;
  }
}
@media (max-height: 900px) {
  .d-fixed {
    padding-top: 80px;
    width: 92%;
    z-index: 9999;
  }
}
/*.d-fixed {*/
/*  padding-top: 50px;*/
/*  width: 92%; position : fixed;*/
/*  bottom: 20px;*/
/*  left: 20px;*/
/*  position: fixed;*/
/*  z-index: 9999;*/
/*}*/
.d-title {
  margin-bottom: 19px;
  color: #fff;
  font-weight: bold;
  font-size: 20px;
}
.d-margin-btn {
  color: #fff;
  margin-bottom: 17px;
}

.d-title2 {
  color: #fff;
  font-weight: bold;
  font-size: 20px;
}

.d-img {
  border-radius: 10px;
  width: 100px;
  height: 100px;
  margin-right: 10px;
}
.d-div {
  color: #fff;
  font-size: 13px;
}
.d-div2 {
  margin-left: 10px;
}

/* 底部 end */
/* 卡片 */
.login-form {
  margin: 0 auto;
  height: 410px;
  width: 440px;
  max-width: 100%;
  padding: 25px 34px;
  position: relative;
  box-sizing: border-box;
  border-radius: 0 5px 5px 0;
  z-index: 2;

  h4 {
    text-align: center;
    margin: 0 0 25px 0;
  }

  & > .el-form-item {
    margin-bottom: 25px;
  }
}

.login-form-right .login-form {
  margin: 0 15% 0 auto;
}

.login-form-left .login-form {
  margin: 0 auto 0 15%;
}

/* 验证码 */
.login-input-group {
  display: flex;
  align-items: center;

  :deep(.el-input) {
    flex: 1;
  }
}

.login-captcha {
  height: 38px;
  width: 102px;
  margin-left: 10px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  text-align: center;
  cursor: pointer;

  &:hover {
    opacity: 0.75;
  }
}

.login-btn {
  display: block;
  width: 100%;
}

/* 第三方登录图标 */
.login-oauth-icon {
  color: #fff;
  padding: 5px;
  margin: 0 10px;
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
}

/* 底部版权 */
.login-copyright {
  color: #eee;
  padding-top: 20px;
  text-align: center;
  position: relative;
  z-index: 1;
}

/* 响应式 */
@media screen and (min-height: 550px) {
  .login-form {
    /*position: absolute;
      top: 50%;
      left: 50%;
      transform: translateX(-50%);
      margin-top: -220px;*/
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: auto;
    right: 15%;
    transform: translateX(0);
    margin: -220px auto auto auto;
  }

  .login-form-left .login-form {
    right: auto;
    left: 15%;
  }

  .login-copyright {
    position: absolute;
    bottom: 20px;
    right: 0;
    left: 0;
  }
}

@media screen and (max-width: 768px) {
  .login-form-right .login-form,
  .login-form-left .login-form {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin-right: auto;
  }
}
</style>
